home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / DEZIP15.ARJ / MEMALLOC.PAS < prev    next >
Pascal/Delphi Source File  |  1989-03-01  |  3KB  |  61 lines

  1. Unit MemAlloc;
  2.  
  3. { Purpose is to provide the ability to create (destroy) dynamic variables  }
  4. { without needing to reserve heap space at compile time.                   }
  5.  
  6. Interface
  7.  
  8. Function Malloc(Var Ptr; Size : Word) : Word;
  9. { Allocate free memory and return a pointer to it.  The amount of memory      }
  10. { requested from DOS is calculated as (Size/4)+1 paragraphs.  If the          }
  11. { allocation is successful, the untyped VAR parameter Ptr will be populated   }
  12. { with the address of the allocated memory block, and the function will return}
  13. { a zero result.  Should the request to DOS fail, Ptr will be populated with  }
  14. { the value NIL, and the function will return the appropriate DOS error code. }
  15.  
  16. Function Dalloc(Var Ptr) : Word;
  17. { Deallocate the memory pointed to by the untyped VAR parameter Ptr           }
  18.  
  19. Implementation
  20.  
  21. Function Malloc(Var Ptr; Size : Word) : Word;
  22. Begin
  23.    Inline(
  24.      $8B/$46/<SIZE/         {            mov         ax,[bp+<Size]}
  25.      $B9/$04/$00/           {            mov         cx,4}
  26.      $D3/$E8/               {            shr         ax,cl}
  27.      $40/                   {            inc         ax}
  28.      $89/$C3/               {            mov         bx,ax}
  29.      $B4/$48/               {            mov         ah,$48}
  30.      $CD/$21/               {            int         $21             ;Allocate memory}
  31.      $72/$07/               {            jc          AllocErr        ;If any errors ....}
  32.      $C7/$46/$FE/$00/$00/   {NoErrors:   mov word    [bp-2],0        ;Return 0 for successful allocation}
  33.      $EB/$05/               {            jmp short   Exit}
  34.      $89/$46/$FE/           {AllocErr:   mov         [bp-2],ax       ;Return error code}
  35.      $31/$C0/               {            xor         ax,ax           ;Store a NIL value into the ptr}
  36.      $C4/$7E/<PTR/          {Exit:       les         di,[bp+<Ptr]    ;Address of pointer into es:di}
  37.      $50/                   {            push        ax              ;Save the Segment part}
  38.      $31/$C0/               {            xor         ax,ax           ;Offset is always 0}
  39.      $FC/                   {            cld                         ;Make sure direction is upward}
  40.      $AB/                   {            stosw                       ;Store offset of memory block}
  41.      $58/                   {            pop         ax              ;Get back segment part}
  42.      $AB);                  {            stosw                       ;Store segment of memory block}
  43.    
  44. End {Malloc};
  45.  
  46. Function Dalloc(Var Ptr) : Word;
  47. Begin
  48.    Inline(
  49.      $B4/$49/               {            mov         ah,$49}
  50.      $C4/$7E/<PTR/          {            les         di,[bp+<Ptr]}
  51.      $26/$C4/$3D/           {        es: les         di,[di]}
  52.      $CD/$21/               {            int         $21}
  53.      $72/$02/               {            jc          Exit}
  54.      $31/$C0/               {NoError:    xor         ax,ax}
  55.      $89/$46/$FE);          {Exit:       mov         [bp-2],ax}
  56.    Pointer(Ptr) := NIL;
  57. End {Dealloc};
  58.  
  59. End {Unit MemAlloc}.
  60.  
  61.